//
//  WATFScanViewController.swift
//  WiFiFaster
//
//  Created by one on 2023/12/28.
//

import UIKit

class WATFScanViewController: WATFBaseViewController {
    init(watf_funcModel: WATFFuncModel? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.watf_funcModel = watf_funcModel
        WATFManager.watf_manager.watf_testAdWork = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(watf_scanView)
        view.addSubview(watf_navView)
        watf_scanView.snp.makeConstraints { make in
            make.top.equalTo(watf_navView.snp.bottom).offset(80)
            make.left.bottom.width.equalToSuperview()
        }
        watf_navView.watf_titleLabel.text = watf_funcModel?.watf_type.watf_title
        if watf_funcModel?.watf_type == .watf_speed {
            watf_speedModel = WATFSpeedModel()
            WATFManager.watf_manager.watf_delegate = self
            WATFManager.watf_manager.watf_startSpeedScan()
            return
        }
        watf_startFuncTimer()
        
        if watf_funcModel?.watf_type == .watf_interference {
            watf_scanView.watf_scanImgView.snp.updateConstraints { make in
                make.size.equalTo(CGSize(width: 107, height: 107))
            }
            watf_scanView.watf_scanBgImgView.image = UIImage(named: "watf_scan_interBg")
            watf_scanView.watf_scanImgView.image = UIImage(named: "watf_scan_intetIcon")
        }
        if watf_funcModel?.watf_type == .watf_idle {
            watf_scanView.watf_scanImgView.snp.updateConstraints { make in
                make.size.equalTo(CGSize(width: 105, height: 105))
            }
            watf_scanView.watf_scanBgImgView.image = UIImage(named: "watf_scan_idleBg")
            watf_scanView.watf_scanImgView.image = UIImage(named: "watf_scan_idleIcon")
        }
        if watf_funcModel?.watf_type == .watf_optimization {
            watf_scanView.watf_scanImgView.snp.updateConstraints { make in
                make.size.equalTo(CGSize(width: 115, height: 115))
            }
            watf_scanView.watf_scanBgImgView.image = UIImage(named: "watf_scan_optimBg")
            watf_scanView.watf_scanImgView.image = UIImage(named: "watf_scan_optimIcon")
            watf_scanView.watf_scanLabel.text = "Optimizing..."
        }
        if watf_funcModel?.watf_type == .watf_speedUP {
            watf_scanView.watf_scanImgView.snp.updateConstraints { make in
                make.size.equalTo(CGSize(width: 107, height: 107))
            }
            watf_scanView.watf_scanImgView.image = UIImage(named: "watf_scan_intetIcon")
        }
       
    }
    
    
    // MARK: - watf lazy
    var watf_speedModel: WATFSpeedModel?
    var watf_funcName: String = "watf_funcName"
    var watf_funcCount:CGFloat = 0
    lazy var watf_navView: WATFNavView = {
        let watf_navView = WATFNavView()
        watf_navView.watf_leftBlock = {[weak self] in
            WATFManager.watf_manager.watf_testAdWork = false
            WATFManager.watf_manager.watf_delegate = nil
            WATFManager.watf_manager.watf_stopSpeedScan()
            if let watf_vcs = self?.navigationController?.children {
                for watf_vc in watf_vcs {
                    if watf_vc.isMember(of: WATFSettingViewController.classForCoder()) {
                        self?.navigationController?.popToViewController(watf_vc, animated: true)
                        return
                    }
                }
            }
            self?.navigationController?.popToRootViewController(animated: true)
        }
        return watf_navView
    }()
    lazy var watf_scanView: WATFScanView = {
        let watf_scanView = WATFScanView()
        return watf_scanView
    }()
   
}
extension WATFScanViewController {
    
    fileprivate func watf_startFuncTimer() {
        watf_funcName = WATFTimerCom.watf_timeTask(self, selector: #selector(watf_scanSum), start: 0, interval: 1, repeats: true, async: false, name: watf_funcName)
        WATFManager.watf_manager.watf_registerBackgroundTask()
    }
    
    fileprivate func watf_stopFuncTimer() {
        WATFTimerCom.watf_cancelTask(watf_funcName)
        watf_funcName = "watf_funcName"
        watf_funcCount = 0
    }
    
    @objc fileprivate func watf_scanSum() {
        watf_funcCount = watf_funcCount + 1
        print(watf_funcCount)
        if (watf_funcModel?.watf_type == .watf_speedUP) && watf_funcCount >= 5 {
            watf_stopFuncTimer()
            navigationController?.pushViewController(WATFUPResultViewController(watf_funcModel: watf_funcModel), animated: true)
            return
        }
        if (watf_funcModel?.watf_type == .watf_interference) && watf_funcCount >= 5 {
            watf_stopFuncTimer()
            navigationController?.pushViewController(WATFInterResultViewController(watf_funcModel: watf_funcModel), animated: true)
            return
        }
        if (watf_funcModel?.watf_type == .watf_idle) && watf_funcCount >= 5 {
            watf_stopFuncTimer()
            navigationController?.pushViewController(WATFIdleResultViewController(watf_funcModel: watf_funcModel), animated: true)
            return
        }
        if (watf_funcModel?.watf_type == .watf_optimization) && watf_funcCount >= 5 {
            watf_stopFuncTimer()
            navigationController?.pushViewController(WATFOptimResultViewController(watf_funcModel: watf_funcModel), animated: true)
            return
        }
    }
    
}

extension WATFScanViewController: WATFManagerToolDelegate {
    func watf_downloadFinish(watf_value: String) {
        watf_speedModel?.watf_download = watf_value
    }
    func watf_downloadProcess(watf_value: String) {
    
        watf_scanView.watf_scanLabel.text = "Download speed \(watf_value)"
    }
    func watf_uploadProcess(watf_value: String) {
        watf_scanView.watf_scanLabel.text = "Upload speed \(watf_value)"
    }
    func watf_upLoadFinish(watf_value: String) {
        watf_speedModel?.watf_upload = watf_value
    }
   
    func watf_pingProcess(watf_ping: String) {
        watf_scanView.watf_scanLabel.text = "ping \(watf_ping)"
    }
    func watf_pingFinish(watf_ping: String, watf_jitter: String, watf_loss: String) {
        WATFManager.watf_manager.watf_delegate = nil
        WATFManager.watf_manager.watf_stopSpeedScan()
        watf_speedModel?.watf_loss = watf_loss
        watf_speedModel?.watf_jitter = watf_jitter
        watf_speedModel?.watf_jitter = watf_jitter
        navigationController?.pushViewController(WATFSpeedResultViewController(watf_resultModel: watf_speedModel, watf_funcModel: watf_funcModel), animated: true)
    }
}
